<?php

namespace App\Services\ApplyDrinkProductOrder;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Order;
use App\Models\Address;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
use App\Services\MakeProductOrder\MakeProductOrder;

class ApplyDrinkProductOrder
{
	protected User $user;

	protected ?Address $address = null;


	public function __construct(User $user, Address $address ,Product $product, string $attr)
	{
		$this->user = $user;
		$this->address = $address;
		$this->product = $product;
		$this->attr = $attr;
	}

	protected function items(): array
	{
		return [[
			'sku_id' => $this->product->skus()->first()->id,
			'amount' => 1
		]];
	}


	protected function createRecordOrder()
	{
		return DB::transaction(function () {

            $order = app(MakeProductOrder::class, [
				'user' => $this->user,
				'address' => $this->address,
				'items' => $this->items(),
				'attr' => $this->attr,
			])->make();

			return $order;
		});
	}

	public function apply()
	{
		return $this->createRecordOrder();
	}
}
